from socket import *
import struct
import os

COMMAND_DICT={'ls':'do_ls','gets':'do_gets','puts':'do_puts',
              'rm':'do_rm','cd':'do_cd','pwd':'do_pwd'}
class Client:
    def __init__(self,ip,port):
        self.client:socket= None
        self.ip=ip
        self.port=port

    def tcp_connect(self):
        self.client=socket(AF_INET,SOCK_STREAM)
        user_name = input('请输入用户名：')
        self.client.connect((self.ip,self.port))
        self.train_send(user_name)


    def send_command(self):
        #读取命令并发送到服务端
        while True:
            command = input()
            self.train_send(command.encode('utf8'))
            if command[:2] == 'ls':
                self.do_ls()
            elif command[:5] == 'rmdir':
                self.do_rmdir(command)
            elif command[:2] == 'cd':
                self.do_cd()
            elif command[:2] == 'rm':
                self.do_rm()
            elif command[:3] == 'pwd':
                self.do_pwd()
            elif command[:4] == 'gets':
                self.do_gets()
            elif command[:4] == 'puts':
                self.do_puts()

            else:
                print('wrong command')


    def do_ls(self):
        data = self.train_recv().decode('utf-8')
        print(data)

    def do_rm(self):
        data = self.train_recv().decode('utf-8')
        print(data)

    def do_cd(self):
        path = self.train_recv().decode('utf-8')
        print(path)

    def do_pwd(self):
        path = self.train_recv().decode('utf-8')
        print(path)

    def do_gets(self):
        flie_name = input('请输入文件名：')
        self.recv_flie(flie_name)


    def do_puts(self):
        pass

    def do_rmdir(self,command):
        data = self.train_recv().decode('utf-8')
        print(data)

    def train_send(self,bytess):
        #pack需要的是一个？
        if not isinstance(bytess, bytes):
            bytess = bytess.encode('utf8')
        train_head = struct.pack('I', len(bytess))
        self.client.send(train_head + bytess)

    def train_recv(self):

        train_head_size = self.client.recv(4)
        train_head = struct.unpack('I',train_head_size)
        return self.client.recv(train_head[0])

    def send_flie(self, flie_name):
        """发送文件"""
        flie_size = os.stat(flie_name).st_size
        # 先发文件大小
        train_head = struct.pack('I', flie_size)
        self.client.send(train_head)
        f = open(flie_name, 'rb')
        while True:
            flie_content = f.read(1000)
            if flie_content:
                self.client.send(flie_content)
            else:
                break
        f.close()

    def recv_flie(self,flie_name):
        """接收文件"""
        #先接文件大小
        self.train_send(flie_name)
        train_head = self.client.recv(4)
        train_head_unpack = struct.unpack('I', train_head)
        flie_size = train_head_unpack[0]
        total = 0
        f = open(flie_name+'接收', 'wb')
        while total < flie_size:
            data = self.client.recv(1000)
            f.write(data)
            total += len(data)
        f.close()



if __name__ == '__main__':
    tcp = Client('192.168.0.8',2000)
    tcp.tcp_connect()
    tcp.send_command()